
package com.azunitech.jms.ejb;

import javax.annotation.Resource;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ejb.ActivationConfigProperty;

import org.apache.log4j.Logger;
	@MessageDriven(
	name = "QueueMDB",
	description = "My First Queue MDB",
	mappedName = "Queue-2",
	messageListenerInterface= MessageListener.class,
	activationConfig = {
	    @ActivationConfigProperty(
	                    propertyName = "destinationType",
	                    propertyValue = "javax.jms.Queue"),
	    @ActivationConfigProperty(
	                    propertyName = "destination",
	                    propertyValue = "queue/Queue-2")
	     }
	)
	public class QueueListenerMDB {
	
		private Logger s_Logger = Logger.getLogger(QueueListenerMDB.class);
	
		@Resource
	    private MessageDrivenContext mdc;
		
		public void onMessage(Message inMessage) throws JMSException {
			s_Logger.info(inMessage.getJMSDestination());
			TextMessage msg = null;
			s_Logger.info("******************************************** " + inMessage.toString());
		      try {
					if (inMessage instanceof TextMessage) {
						msg = (TextMessage) inMessage;
						deliverTicket(msg); 
					}
	         } 
	         catch (JMSException e) {
	             e.printStackTrace();
	             mdc.setRollbackOnly();
	         } 
	         catch (Throwable te) {
	             te.printStackTrace();
	         }
		}
	
	protected TopicConnectionFactory tconFactory;
	protected TopicConnection tconn;
	protected TopicSession tsession;
	protected TopicPublisher tpublisher;
	protected Topic topic;
	protected TextMessage msg;
	
    public void deliverTicket(TextMessage msg) throws NamingException, JMSException{
    	InitialContext iniCtx = new InitialContext();
        Object tmp = iniCtx.lookup("CF-COMMAND");
        TopicConnectionFactory qcf = (TopicConnectionFactory) tmp;
        tconn = qcf.createTopicConnection();
        tsession = tconn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        tconn.start();
        
        s_Logger.info("TextMDB.onMessage, this=" + hashCode());
        try {
            TextMessage tm = (TextMessage) msg;
            String text = tm.getText() + "processed by: "+hashCode();
            
            Topic topic = (Topic)iniCtx.lookup("Topic-1");
            TopicPublisher tpublisher = tsession.createPublisher(topic);
    		TextMessage textMessage = tsession.createTextMessage();
    		textMessage.setText(msg.getText());
    		tpublisher.send(textMessage);
  
        } catch(Throwable t) {
            t.printStackTrace();
        }
 	}
}
